/*
#          .,:,,,                                        .::,,,::.          
#        .::::,,;;,                                  .,;;:,,....:i:         
#        :i,.::::,;i:.      ....,,:::::::::,....   .;i:,.  ......;i.        
#        :;..:::;::::i;,,:::;:,,,,,,,,,,..,.,,:::iri:. .,:irsr:,.;i.        
#        ;;..,::::;;;;ri,,,.                    ..,,:;s1s1ssrr;,.;r,        
#        :;. ,::;ii;:,     . ...................     .;iirri;;;,,;i,        
#        ,i. .;ri:.   ... ............................  .,,:;:,,,;i:        
#        :s,.;r:... ....................................... .::;::s;        
#        ,1r::. .............,,,.,,:,,........................,;iir;        
#        ,s;...........     ..::.,;:,,.          ...............,;1s        
#       :i,..,.              .,:,,::,.          .......... .......;1,       
#      ir,....:rrssr;:,       ,,.,::.     .r5S9989398G95hr;. ....,.:s,      
#     ;r,..,s9855513XHAG3i   .,,,,,,,.  ,S931,.,,.;s;s&BHHA8s.,..,..:r:     
#    :r;..rGGh,  :SAG;;G@BS:.,,,,,,,,,.r83:      hHH1sXMBHHHM3..,,,,.ir.    
#   ,si,.1GS,   sBMAAX&MBMB5,,,,,,:,,.:&8       3@HXHBMBHBBH#X,.,,,,,,rr    
#   ;1:,,SH:   .A@&&B#&8H#BS,,,,,,,,,.,5XS,     3@MHABM&59M#As..,,,,:,is,   
#  .rr,,,;9&1   hBHHBB&8AMGr,,,,,,,,,,,:h&&9s;   r9&BMHBHMB9:  . .,,,,;ri.  
#  :1:....:5&XSi;r8BMBHHA9r:,......,,,,:ii19GG88899XHHH&GSr.      ...,:rs.  
#  ;s.     .:sS8G8GG889hi.        ....,,:;:,.:irssrriii:,.        ...,,i1,  
#  ;1,         ..,....,,isssi;,        .,,.                      ....,.i1,  
#  ;h:               i9HHBMBBHAX9:         .                     ...,,,rs,  
#  ,1i..            :A#MBBBBMHB #  .r1,..        ,..;3BMBBBHBB#Bh.     ..                    ....,,,,,i1;   
#   :h;..       .,..;,1XBMMMMBXs,.,, .. :: ,.               ....,,,,,,ss.   
#    ih: ..    .;;;, ;;:s58A3i,..    ,. ,.:,,.             ...,,,,,:,s1,    
#    .s1,....   .,;sh,  ,iSAXs;.    ,.  ,,.i85            ...,,,,,,:i1;     
#     .rh: ...     rXG9XBBM#M#MHAX3hss13&&HHXr         .....,,,,,,,ih;      
#      .s5: .....    i598X&&A&AAAAAA&XG851r:       ........,,,,:,,sh;       
#      . ihr, ...  .         ..                    ........,,,,,;11:.       
#         ,s1i. ...  ..,,,..,,,.,,.,,.,..       ........,,.,,.;s5i.         
#          .:s1r,......................       ..............;shs,           
#          . .:shr:.  ....                 ..............,ishs.             
#              .,issr;,... ...........................,is1s;.               
#                 .,is1si;:,....................,:;ir1sr;,                  
#                    ..:isssssrrii;::::::;;iirsssssr;:..                    
#                         .,::iiirsssssssssrri;;:.
*/
package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingMapper;
import com.itheima.health.exception.BusinessException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * <dl>
 * <dd>描述: ~ OrderSettingServiceImpl</dd>
 * <dd>创建时间：  10:13 2021/2/28</dd>
 * <dd>创建人： guodong</dd>
 * <dt>版本历史: </dt>
 * <pre>
 * Date         Author         Version     Description
 * ------------------------------------------------------------------
 * 2021/2/28      guodong         1.0        1.0 Version
 * </pre>
 * </dl>
 */
@DubboService
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    /**
     * @Author: guodong
     * @Date: 10:12 2021/2/28
     * @ClassName: OrderSettingService
     * @Parms [orderSettingList]
     * @ReturnType: void
     * @Description: excel批量上传设置可预约人数
     */
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        for (OrderSetting orderSetting : orderSettingList) {
            orderSettingMapper.insertSelective(orderSetting);
        }
    }

    /**
     * @Author: guodong
     * @Date: 11:06 2021/2/28
     * @ClassName: OrderSettingController
     * @Parms [startTime, endTime]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 根据日期查询当月预约信息
     */
    @Override
    public List<OrderSetting> findByDate(String startTime, String endTime) {
        Example example = new Example(OrderSetting.class);
        Example.Criteria criteria = example.createCriteria();
        //SELECT * FROM `t_ordersetting` WHERE orderDate BETWEEN '2021-03-01' AND '2021-3-31'
        criteria.andBetween("orderDate", startTime, endTime);
        //SELECT * FROM `t_ordersetting` WHERE orderDate >='2021-03-01' AND orderDate<= '2021-3-31'
       /* criteria.andGreaterThanOrEqualTo("orderDate", startTime);
        criteria.andLessThanOrEqualTo("orderDate", endTime);*/
        return orderSettingMapper.selectByExample(example);
    }

    /**
     * @Author: guodong
     * @Date: 11:46 2021/2/28
     * @ClassName: OrderSettingController
     * @Parms [orderSetting]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 设置可预约人数
     */
    @Override
    public void editNumByDate(OrderSetting orderSetting) {
        //1.根据日期查询预约 ---> select * from t_ordersetting where orderDate = "2021-03-06" and number=100
        OrderSetting orderSetting1 = new OrderSetting();
        orderSetting1.setOrderDate(orderSetting.getOrderDate());
        OrderSetting selectOne = orderSettingMapper.selectOne(orderSetting1);
        //2.如果不存在那么新增
        if(null == selectOne){
            orderSettingMapper.insertSelective(orderSetting);
        }else {//3.如果存在---》判断可预约人数与已预约人数的关系
            //4.可预约人数<已预约人数--->抛出异常
            if(orderSetting.getNumber() < selectOne.getReservations()){
                throw new BusinessException("可预约人数不能小于已预约人数！");
            }
            //5.修改
            orderSetting.setId(selectOne.getId()); //此处需要注意必须设置主键
            orderSettingMapper.updateByPrimaryKeySelective(orderSetting);
        }
    }
}
